package com.woniu108.product.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.ServiceInfoDto;
import com.woniu108.carService.commos.dto.StoreAdminDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.produnt.ProductException;
import com.woniu108.carService.commos.exception.produnt.ProductExceptionCode;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.product.client.PersonClient;
import com.woniu108.product.mapper.ServiceInfoMapper;
import com.woniu108.product.mapper.ServiceTypeMapper;
import com.woniu108.product.model.ServiceInfo;
import com.woniu108.product.model.ServiceType;
import com.woniu108.product.param.*;
import com.woniu108.product.service.ServiceInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务信息表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class ServiceInfoServiceImpl extends ServiceImpl<ServiceInfoMapper, ServiceInfo> implements ServiceInfoService {
    @Resource
    private ServiceInfoMapper serviceInfoMapper;
    @Resource
    private ServiceTypeMapper serviceTypeMapper;
    @Resource
    private PersonClient personClient;

    /**
     * 判断店家
     */
    private Integer isStore(String storeAdminId) {
        // 检查 adminId 是否为 null
        if (storeAdminId == null) {
            throw new AdminException(AdminExceptionCode.STORE_ADMIN_ID_NULL);
        }
        // 判断店家权限
        StoreAdminDto dto = personClient.getStoreAdminInfoById(storeAdminId).getData();
        if (dto == null) {
            throw new AdminException(AdminExceptionCode.STORE_ADMIN_NOT_HAVE);
        }
        if (!Objects.equals(dto.getStoreAdminState(), StaticData.ADMIN_STATE_NORMAL)) {
            throw new AdminException(AdminExceptionCode.STORE_ADMIN_STATE_ERROR);
        }
        return dto.getStoreId();
    }

    @Override
    public List<ServiceInfoDto> findAllServiceInfo(Integer sTid, Integer storeId) {
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        //有种类id就查该种类，没有就查全部
        if (sTid != null) {
            wrapper.eq("s_t_id", sTid);
        }
        //有门店id就查，没有就查全部
        if (storeId != null) {
            wrapper.eq("store_id", storeId);
        }
        wrapper.eq("service_state", StaticData.SERVICE_INFO_STATE_NORMAL);
        List<ServiceInfo> infos = serviceInfoMapper.selectList(wrapper);
        //设dto集合
        ArrayList<ServiceInfoDto> dtos = new ArrayList<>();
        for (ServiceInfo info : infos) {
            ServiceInfoDto dto = BeanUtil.toBean(info, ServiceInfoDto.class);
            //查服务种类名
            ServiceType type = serviceTypeMapper.selectById(dto.getSTId());
            if (type == null) {
                continue;
            }
            dto.setSTName(type.getSTName());
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<ServiceInfoDto> findMyServiceInfo(String storeAdminId) {
        //判断店家id,获得门店id
        Integer storeId = isStore(storeAdminId);
        //查该门店全部服务
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId)
                .ne("service_state", StaticData.SERVICE_INFO_STATE_DELETE);
        List<ServiceInfo> infos = serviceInfoMapper.selectList(wrapper);
        //设dto集合
        ArrayList<ServiceInfoDto> dtos = new ArrayList<>();
        for (ServiceInfo info : infos) {
            ServiceInfoDto dto = BeanUtil.toBean(info, ServiceInfoDto.class);
            //查服务种类名
            ServiceType type = serviceTypeMapper.selectById(info.getSTId());
            if (type == null) {
                continue;
            }
            dto.setSTName(type.getSTName());
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public Boolean insertServiceInfo(InsertServiceInfoParam param) {
        //判断店家id,获得门店id
        Integer storeId = isStore(param.getStoreAdminId());
        //查该服务名是否存在,通过名称和门店id
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("service_name", param.getServiceName())
                .eq("store_id", storeId);
        ServiceInfo selectOne = serviceInfoMapper.selectOne(wrapper);
        //判断该服务存在和状态
        if (selectOne != null) {
            if (Objects.equals(selectOne.getServiceState(), StaticData.SERVICE_INFO_STATE_BAN) || Objects.equals(selectOne.getServiceState(), StaticData.SERVICE_INFO_STATE_NORMAL)) {
                throw new ProductException(ProductExceptionCode.SERVICE_INFO_HAVE);
            }
            //将删除状态改为正常
            selectOne.setServiceState(StaticData.SERVICE_INFO_STATE_NORMAL);
            // 设置 param 中的值给 selectOne
            selectOne.setServiceName(param.getServiceName());
            selectOne.setSTId(param.getSTId());
            selectOne.setServiceDetails(param.getServiceDetails());
            selectOne.setPayNum(0);
            selectOne.setCreateTime(LocalDateTime.now());
            selectOne.setServiceBasePrice(param.getServiceBasePrice());
            selectOne.setStreamId(param.getStreamId());
            int i = serviceInfoMapper.updateById(selectOne);
            return i > 0;
        }
        // 如果 selectOne 为空，创建一个新的 ProductInfo 对象
        ServiceInfo info = BeanUtil.toBean(param, ServiceInfo.class);
        info.setPayNum(0);
        info.setStoreId(storeId);
        info.setCreateTime(LocalDateTime.now());
        info.setServiceState(StaticData.SERVICE_INFO_STATE_NORMAL);
        int insert = serviceInfoMapper.insert(info);
        return insert > 0;
    }

    @Override
    public Boolean updateServiceInfo(UpdateServiceInfoParam param) {
        //判断店家id,获得门店id
        Integer storeId = isStore(param.getStoreAdminId());
        //通过id查服务
        ServiceInfo info = serviceInfoMapper.selectById(param.getServiceId());
        if (info == null || Objects.equals(info.getServiceState(), StaticData.SERVICE_INFO_STATE_DELETE)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_NOT_HAVE);
        }
        //通过名字查，看新名字是否同名
        if (param.getServiceName() != null) {
            QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("service_name", param.getServiceName());
            ServiceInfo selectOne = serviceInfoMapper.selectOne(wrapper);
            if (selectOne != null) {
                throw new ProductException(ProductExceptionCode.SERVICE_INFO_HAVE);
            }
            //修改名字
            info.setServiceName(param.getServiceName());
        }

        //修改价格
        if (param.getServiceBasePrice() != null) {
            info.setServiceBasePrice(param.getServiceBasePrice());
        }

        //修改描述
        if (param.getServiceDetails() != null) {
            info.setServiceDetails(param.getServiceDetails());
        }
        int i = serviceInfoMapper.updateById(info);
        return i > 0;

    }

    @Override
    public Boolean banServiceInfo(BanServiceInfoParam param) {
        //判断店家id,获得门店id
        Integer storeId = isStore(param.getStoreAdminId());
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("service_id",param.getServiceId());
        //禁用商品详情
        ServiceInfo serviceInfo = serviceInfoMapper.selectOne(wrapper);
        System.out.println("serviceInfo"+serviceInfo);
        //判断门店id
        if (!Objects.equals(serviceInfo.getStoreId(), storeId)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_NOT_YOU_STORE);
        }
        // 判断是否存在和状态

        if (Objects.equals(serviceInfo.getServiceState(), StaticData.SERVICE_INFO_STATE_DELETE)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_NOT_HAVE);
        }

        if (Objects.equals(serviceInfo.getServiceState(), StaticData.SERVICE_INFO_STATE_BAN)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_BANED);
        }
        //禁用
        serviceInfo.setServiceState(StaticData.SERVICE_INFO_STATE_BAN);
        serviceInfoMapper.updateById(serviceInfo);
        return true;
    }

    @Override
    public Boolean unlockServiceInfo(UnlockServiceInfoParam param) {
        //判断店家id,获得门店id
        Integer storeId = isStore(param.getStoreAdminId());
        //解锁商品详情
        ServiceInfo serviceInfo = serviceInfoMapper.selectById(param.getServiceId());
        //判断门店id
        if (!Objects.equals(serviceInfo.getStoreId(), storeId)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_NOT_YOU_STORE);
        }
        // 判断是否存在和状态

        if (Objects.equals(serviceInfo.getServiceState(), StaticData.SERVICE_INFO_STATE_DELETE)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_NOT_HAVE);
        }

        if (Objects.equals(serviceInfo.getServiceState(), StaticData.SERVICE_INFO_STATE_NORMAL)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_STATE_NORMAL);
        }
        //解锁用
        serviceInfo.setServiceState(StaticData.SERVICE_INFO_STATE_NORMAL);
        serviceInfoMapper.updateById(serviceInfo);
        return true;
    }

    @Override
    public Boolean deleteServiceInfo(DeleteServiceInfoParam param) {
        //判断店家id,获得门店id
        Integer storeId = isStore(param.getStoreAdminId());
        // 查找商品详情
        ServiceInfo serviceInfo = serviceInfoMapper.selectById(param.getServiceId());

        // 判断商品详情是否存在和状态
        if (serviceInfo == null || Objects.equals(serviceInfo.getServiceState(), StaticData.SERVICE_INFO_STATE_DELETE)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_NOT_HAVE);
        }
        //判断门店id
        if (!Objects.equals(serviceInfo.getStoreId(), storeId)) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_NOT_YOU_STORE);
        }
        // 删除
        serviceInfo.setServiceState(StaticData.SERVICE_INFO_STATE_DELETE);
        serviceInfoMapper.updateById(serviceInfo);
        return true;
    }

    @Override
    public ServiceInfoDto findServiceInfoById(Integer serviceId) {
        ServiceInfo serviceInfo = serviceInfoMapper.selectById(serviceId);
        ServiceInfoDto dto = BeanUtil.toBean(serviceInfo, ServiceInfoDto.class);
        //查服务种类名
        ServiceType type = serviceTypeMapper.selectById(serviceInfo.getSTId());
        if (type == null) {
            throw new ProductException(ProductExceptionCode.SERVICE_INFO_NOT_HAVE);
        }
        dto.setSTName(type.getSTName());
        return dto;
    }

    @Override
    public Boolean findServiceInfoByStreamId(Integer streamId) {
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("stream_id",streamId)
                .ne("service_state",StaticData.SERVICE_INFO_STATE_DELETE);
        ServiceInfo selectOne = serviceInfoMapper.selectOne(wrapper);
        if (selectOne==null){
            throw new ProductException(500,"不存在服务使用此流程");
        }
        return true;
    }
}
